package com.example.weixin_01.test.design_model.proxy;

import java.lang.reflect.Proxy;

/**
 * @Author: zhenghuisheng
 * @Date: 2022/9/10 18:09
 */
public class User {
    public static void main(String[] args) {
        //用户手机店购买手机
//        Shop shop = new Shop();
//        Double price = shop.sell(1);
//        System.out.println(price);
        //获取掉这个动态代理对象

        //1，创建目标对象
        PhoneSell factory = new Factory();
        //2，创建proxyInvocationHandler对象
        ProxyInvocationHandler proxyInvocationHandler = new ProxyInvocationHandler(factory);
        //3，创建动态代理对象
        /**
         * newProxyInstance(ClassLoader loader,
         *                 Class<?>[] interfaces,
         *                 InvocationHandler h)
         */
        //获取目标类类加载器
        ClassLoader classLoader = factory.getClass().getClassLoader();
        //获取类对应的接口
        Class<?>[] interfaces = factory.getClass().getInterfaces();

        //强行转换成对应的接口
        PhoneSell proxy = (PhoneSell)Proxy.newProxyInstance(classLoader, interfaces, proxyInvocationHandler);

        //com.sun.proxy.$Proxy0  jdk固定对象
        System.out.println(proxy.getClass().getName());

        //购买数量
        Double sellPrice = proxy.sell(4);

        System.out.println(sellPrice);

    }
}
